package com.lanou.tipsy.adapter.songList;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.lanou.tipsy.R;
import com.lanou.tipsy.entity.songList.songslist.ChildrenEntity;
import com.lanou.tipsy.entity.songList.songslist.RankDataEntity;
import com.lanou.tipsy.tools.listview.NoScrollGridView;

import java.util.ArrayList;

/**
 * Created by dllo on 15/11/4.
 *
 *   歌单分类菜单  PopupWindow
 */
public class SongListPopupWindow extends PopupWindow implements View.OnClickListener {
    //声明布局
    private View mMenuView;
    private Activity context;
    private SongListMenuAdapter adapter001,adapter002,adapter003,adapter004,adapter005;
    private NoScrollGridView gridView1,gridView2,gridView3,gridView4,gridView5;
    private TextView textView01,textView02,textView03,textView04,textView05,cancelTv,allListMenuTv;
    private RequestQueue requestQueue;
    private ArrayList<RankDataEntity> menuData;
    private ArrayList<ChildrenEntity> childrenEntity,childrenEntity1,childrenEntity2,childrenEntity3,childrenEntity4,childrenEntity5;
    private String tag1,tag2,tag3,tag4,tag5,allTag;
    private Context mContext;
    public SongListPopupWindow(Activity context,AdapterView.OnItemClickListener itemsOnClick,
                               View.OnClickListener onClick,ArrayList<RankDataEntity> menuData){
        mContext = context;
        this.menuData = menuData;
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mMenuView = inflater.inflate(R.layout.fr_songlist_menu,null);
        gridView1 = (NoScrollGridView) mMenuView.findViewById(R.id.songListGV_1);
        gridView2 = (NoScrollGridView) mMenuView.findViewById(R.id.songListGV_2);
        gridView3 = (NoScrollGridView) mMenuView.findViewById(R.id.songListGV_3);
        gridView4 = (NoScrollGridView) mMenuView.findViewById(R.id.songListGV_4);
        gridView5 = (NoScrollGridView) mMenuView.findViewById(R.id.songListGV_5);
        textView01 = (TextView) mMenuView.findViewById(R.id.text01);
        textView02 = (TextView) mMenuView.findViewById(R.id.text02);
        textView03 = (TextView) mMenuView.findViewById(R.id.text03);
        textView04 = (TextView) mMenuView.findViewById(R.id.text04);
        textView05 = (TextView) mMenuView.findViewById(R.id.text05);

        allListMenuTv = (TextView) mMenuView.findViewById(R.id.allListMenuTv);
        cancelTv = (TextView) mMenuView.findViewById(R.id.cancelTv);

        gridView1.setOnItemClickListener(itemsOnClick);
        gridView2.setOnItemClickListener(itemsOnClick);
        gridView3.setOnItemClickListener(itemsOnClick);
        gridView4.setOnItemClickListener(itemsOnClick);
        gridView5.setOnItemClickListener(itemsOnClick);

        allListMenuTv.setOnClickListener(onClick);
        cancelTv.setOnClickListener(this);
        //设置按钮监听
        //设置SelectPicPopupWindow的View
        this.setContentView(mMenuView);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(800);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);

        this.showAtLocation(mMenuView, Gravity.BOTTOM,0,0);
        //设置SelectPicPopupWindow弹出窗体动画效果
        //   this.setAnimationStyle(R.style.AnimBottom);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0xb0000000);

        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        //产生背景变暗效果
        WindowManager.LayoutParams lp = context.getWindow().getAttributes();
        lp.alpha = 0.4f;
        context.getWindow().setAttributes(lp);
//取消按钮匿名监听
        cancelTv.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                dismiss();
            }
        });

        //mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
        mMenuView.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                int height = mMenuView.findViewById(R.id.songList).getTop();
                Log.i("销毁菜单 点击", "OnTouchListener");
                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (y < height) {
                        update();
                        dismiss();
                    }
                }
                return true;
            }
        });
        initData();
        initAdapter();

    }
    public void initAdapter(){
        adapter001 = new SongListMenuAdapter(mContext,childrenEntity1);
        gridView1 = (NoScrollGridView) mMenuView.findViewById(R.id.songListGV_1);
        gridView1.setAdapter(adapter001);

        adapter002 = new SongListMenuAdapter(mContext,childrenEntity2);
        gridView2 = (NoScrollGridView) mMenuView.findViewById(R.id.songListGV_2);
        gridView2.setAdapter(adapter002);

        adapter003 = new SongListMenuAdapter(mContext,childrenEntity3);
        gridView3 = (NoScrollGridView) mMenuView.findViewById(R.id.songListGV_3);
        gridView3.setAdapter(adapter003);

        adapter004 = new SongListMenuAdapter(mContext,childrenEntity4);
        gridView4 = (NoScrollGridView) mMenuView.findViewById(R.id.songListGV_4);
        gridView4.setAdapter(adapter004);

        adapter005 = new SongListMenuAdapter(mContext,childrenEntity5);
        gridView5 = (NoScrollGridView) mMenuView.findViewById(R.id.songListGV_5);
        gridView5.setAdapter(adapter005);
    }
    public void initData() {
        if (menuData != null) {
            childrenEntity1 = menuData.get(0).getChildren();
            childrenEntity2 = menuData.get(1).getChildren();
            childrenEntity3 = menuData.get(2).getChildren();
            childrenEntity4 = menuData.get(3).getChildren();
            childrenEntity5 = menuData.get(4).getChildren();

            tag1 = menuData.get(0).getTag();
            tag2 = menuData.get(1).getTag();
            tag3 = menuData.get(2).getTag();
            tag4 = menuData.get(3).getTag();
            tag5 = menuData.get(4).getTag();

            textView01.setText(tag1);
            textView02.setText(tag2);
            textView03.setText(tag3);
            textView04.setText(tag4);
            textView05.setText(tag5);
        }
    }

    @Override
    public void onClick(View v) {

    }
}
